package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.DmcCsunSleepConfigMapper;
import com.csun.cmny.provider.model.constant.LifeConstant;
import com.csun.cmny.provider.model.domain.*;
import com.csun.cmny.provider.model.dto.csunsleep.SleepConfigAndThresholdDto;
import com.csun.cmny.provider.model.vo.csunsleep.SleepConfigAndThresholdVO;
import com.csun.cmny.provider.service.DmcCsunSleepConfigService;
import com.csun.cmny.provider.service.DmcCsunSleepThresholdService;
import com.csun.cmny.provider.service.DmcDeviceUserBindService;
import com.csun.cmny.util.PublicUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
@Transactional
public class DmcCsunSleepConfigServiceImpl extends BaseService<DmcCsunSleepConfig> implements DmcCsunSleepConfigService {
    @Resource
    private DmcCsunSleepConfigMapper dmcCsunSleepConfigMapper;
    @Resource
    private DmcCsunSleepThresholdService dmcCsunSleepThresholdService;
    @Resource
    private DmcDeviceUserBindService deviceUserBindService;

    @Override
    public int saveOrUpdate(DmcCsunSleepConfig csunSleepConfig) {

        DmcCsunSleepConfig config = dmcCsunSleepConfigMapper.selectByPrimaryKey(csunSleepConfig.getDeviceId());
        if(config == null){
            return dmcCsunSleepConfigMapper.insertSelective(csunSleepConfig);
        }else{
            return dmcCsunSleepConfigMapper.updateByPrimaryKeySelective(csunSleepConfig);
        }
    }

    @Override
    public int setCsunSleepConfigAndThreshold(SleepConfigAndThresholdDto dto, LoginAuthDto loginAuthDto) {

        List<Long> deviceIdList = dto.getDeviceIdList();
        int i = 0;
        
        for (Long deviceId : deviceIdList) {

            //1.下发时间指令
            DmcCsunSleepConfig csunHumidistatConfig = selectByKey(deviceId);
            DmcCsunSleepConfig config = new DmcCsunSleepConfig();

            config.setDeviceId(deviceId);
            config.setVitalMeasuringTimeInterval(dto.getVitalMeasuringTimeInterval());
            config.setIsSend(LifeConstant.DEVICE_ORDER_NEED_SEND);
            config.setLastOperator(loginAuthDto.getUserName());
            config.setLastOperatorId(loginAuthDto.getUserId());

            if(csunHumidistatConfig == null){
                config.setCreator(loginAuthDto.getUserName());
                config.setCreatorId(loginAuthDto.getUserId());
                dmcCsunSleepConfigMapper.insertSelective(config);
            }else{
                dmcCsunSleepConfigMapper.updateByPrimaryKeySelective(config);
            }

            //2.设置阈值
            DmcCsunSleepThreshold dmcCsunSleepThreshold = dmcCsunSleepThresholdService.selectByKey(deviceId);

            //同步到个人用户设备绑定表
            DmcDeviceUserBind deviceUserBind = new DmcDeviceUserBind();
            deviceUserBind.setDeviceId(deviceId);
            DmcDeviceUserBind userBind = deviceUserBindService.selectOne(deviceUserBind);

            if (PublicUtil.isNotEmpty(userBind)){
                BeanUtils.copyProperties(dto,deviceUserBind);
                deviceUserBindService.update(deviceUserBind);
            }


            DmcCsunSleepThreshold threshold = new DmcCsunSleepThreshold();

            threshold.setDeviceId(deviceId);
            BeanUtils.copyProperties(dto,threshold);
            threshold.setLastOperator(loginAuthDto.getUserName());
            threshold.setLastOperatorId(loginAuthDto.getUserId());

            if(dmcCsunSleepThreshold == null){
                threshold.setCreator(loginAuthDto.getUserName());
                threshold.setCreatorId(loginAuthDto.getUserId());
                dmcCsunSleepThresholdService.save(threshold);
            }else{
                dmcCsunSleepThresholdService.update(threshold);
            }
            i++;
        }
        return i;
    }

    @Override
    public SleepConfigAndThresholdVO getCsunSleepConfigAndThreshold(Long deviceId) {

        SleepConfigAndThresholdVO vo = new SleepConfigAndThresholdVO();
        DmcCsunSleepConfig dmcCsunSleepConfig = dmcCsunSleepConfigMapper.selectByPrimaryKey(deviceId);
        DmcCsunSleepThreshold dmcCsunSleepThreshold = dmcCsunSleepThresholdService.selectByKey(deviceId);
        if(dmcCsunSleepConfig != null){
            BeanUtils.copyProperties(dmcCsunSleepConfig,vo);
        }
        if(dmcCsunSleepThreshold != null){
            BeanUtils.copyProperties(dmcCsunSleepThreshold,vo);
        }

        return vo;
    }
}
